// pages/menu0/menu0.js
import {request} from '../../utils/api.js'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
     name:'',
     phone:'',
     lock:'',
	 submit:{
		 lock:[],
		 thing:'',
     date:'',
      date1:'',
		 company:{
       company:'',
       id:''
     }
	 }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  getuser(){
            const data=wx.getStorageSync('userinfo');
            let mobile=data.mobile; let name=data.real_name;
             if(mobile==null||name==null){
			  mobile="";
			  name='';
             }
             this.setData({
               name:name,
               phone:mobile
             })
  },
  onLoad: function (options) {
       this.getuser();
        app.globalData.company=null;
        app.globalData.lock=[];
  },
  inputcompnay(e){
     this.setData({
       'submit.company.company':e.detail.value
     })
  },
  choosecompany(){
    
    if(!this.data.submit.company){
      wx.showModal({
        content:'请输入单位'
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/search/search?keywords='+this.data.submit.company.company,
    })
  },
  inputlock(e){
    this.setData({
      lock:e.detail.value
    })
  },
  searchlock(){
       if(!this.data.submit.company){
           wx.showModal({
            title: '请选择单位！'
           })
           return;
       }
       if(!this.data.lock){
        wx.showToast({
          icon:'none',   
          title: '请输入门锁！',
        })
        return;
    }
         const id=this.data.submit.company.id;
         const name=this.data.lock;
        
          wx.navigateTo({
               url:`/pages/siteserach/siteserach?id=${id}&name=${name}`
             })
    
  },
  bindInput(e){
	this.setData({
	     'submit.thing':e.detail.value
	    })
  },
  bindDateChange(e){
       
	 this.setData({
	      'submit.date':e.detail.value
	     })
  },
  bindDateChange1(e){
    this.setData({
         'submit.date1':e.detail.value
        })
},
submit(){
  if(!this.data.submit.company){
    wx.showToast({
         icon:'none',   
         title: '请选择单位',
       })
       return;
  }
   if(!this.data.submit.company.id){
     wx.showToast({
          icon:'none',   
          title: '请选择单位',
        })
        return;
   }
   if(!this.data.submit.lock){
     wx.showToast({
          icon:'none',   
          title: '请选择门锁',
        })
        return;
   }
  
   if(!this.data.submit.thing){ 
     wx.showToast({
          icon:'none',   
          title: '请选择事由',
        })
        return;
   }
   if(!this.data.submit.date&&!this.data.submit.date1){
      wx.showToast({
        icon:'none',   
        title: '请选择时间段'
      })
      return;
   }
   let lockid='';
   this.data.submit.lock.forEach(item=>{
        lockid+=item.id+','
   })
   const date=new Date(this.data.submit.date).getTime()+'-'+new Date(this.data.submit.date1).getTime();
   request({
        url:'/api/wxapi/openlock_apply',
        header:{
             token:wx.getStorageSync('token')
        },
        data:{
          apply_mark:this.data.submit.thing,
          time:date,
          lockid:lockid,
          admin_id:this.data.submit.company.id
        }
   }).then(res=>{
        if(res.data.code==1){
             wx.showToast({
               icon:'none',
               title: res.data.msg
             })
             
        }
   })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     this.setData({
        'submit.company':app.globalData.company,
        'submit.lock':app.globalData.lock
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})